#!/bin/bash
 # ------------------------------------------------------------------
 # Author: 
 # Title: cesm-loop_startup.sh
 # Description: Loop over list of initial files
 #              Clone a CESM case and submit a startup simulation
 # ------------------------------------------------------------------
 USAGE="Usage: $0 CASEprefix CLONEROOT CLONEROOT2 START"
# OPTIONS="res [default:f19_f19], compset [default:F_2000], ncores [default:128] in that order"
 # --- Options processing -------------------------------------------
 if [ $# -ne 4 ] ; then
     echo $USAGE
#     echo $OPTIONS
     exit 1;
 fi
 CASEprefix=$1
 shopt -s extglob
 CLONEROOT=${2%%+(/)}
 CLONEROOT2=${3%%+(/)}
 START=$4
 # Accept arguments with defaults:
# RES=${2:-"f19_f19"}
# COMPSET=${3:-"F_2000"}
# NCORES=${4:-"128"}
 MACHINE="odyssey"
# echo MACHINE!!!!!!!$MACHINE
# echo RES:$RES
# echo COMPSET:$COMPSET
 #module
 #module restore runmodel
# grep 'impi/5.1.2.150-fasrc01/netcdf/4.1.3-fasrc09' <<< "$LD_LIBRARY_PATH" >/dev/null
 grep 'impi/2017.2.174-fasrc01/netcdf/4.1.3-fasrc02' <<< "$LD_LIBRARY_PATH" >/dev/null
 if [ $? -ne 0 ]; then
   echo
   echo module mismatch!
   echo Loading modules ... takes half a minute ...
   echo load modules beforehand next time ^_^
   echo edit $0 if this behaviour unwanted
#   source new-modules.sh
   module purge
#   module load intel/15.0.0-fasrc01 impi/5.1.2.150-fasrc01 netcdf/4.1.3-fasrc09
#   module load perl-modules/5.22.0-fasrc03
   module load intel/17.0.4-fasrc01 impi/2017.2.174-fasrc01 netcdf/4.1.3-fasrc02
   module load perl-modules/5.10.1-fasrc13
 fi
 export COMPILER="intel"
 module load nco
 #libs and includes
 #export INC_NETCDF="/n/sw/centos6/netcdf-4.3.0_intel-13.0.079/include"
 #export LIB_NETCDF="/n/sw/centos6/netcdf-4.3.0_intel-13.0.079/lib"
 #export INC_MPI="/n/sw/centos6/openmpi-1.7.2_intel-13.0.079/include"
 #export LIB_MPI="/n/sw/centos6/openmpi-1.7.2_intel-13.0.079/lib"
#if [ $START == 1 ]; then

#rm ~/forcing_txt/wide/*_drft.txt
#
#sbatch ~/runtextwrite0.sh

#fi

#while [ ! -f ~/forcing_txt/wide/T_drft.txt ]; do
#	sleep 1m
#done

# SCRIPTDIR=$PWD
# CESMROOT="/glade/p/cesm"
# CESMDATAROOT="/glade/p/cesmdata/cseg"

# BRCPATH="/n/home05/pchan/zlf/cesm_output/"  #TODO
# BRCCASE="c3rest"
# for BRCDATE in `\ls -1 ${BRCPATH}/${BRCCASE}/rest/ |cut -c 1-10`; do
# for ncdata in /n/holylfs/LABS/kuang_lab/leiw/CAM_input/atm/cam/inic/fv/cami_0000-01-01_1.9x2.5_L26_c070408_*nc; do
for i in $(seq $START 2400); do


#if (( $i % 8 == 1  ))
#then

#rm ~/forcing_txt/wide/*_drft.txt

#sbatch ~/runtextwrite0.sh

#fi

#while [ ! -f ~/forcing_txt/wide/T_drft.txt ]; do
#	sleep 15s
#done


casenum=0
 cd $CLONEROOT
 clonebld=`./xmlquery EXEROOT -s -valonly`
NCES=""
# for ncdata in /n/regal/kuang_lab/leiw/MERRA2_ANA/IC/MERRA2_ana_IC_????0115_interpolated.nc; do
for ncdata in ~nedkleiner/kuanglfs/MERRAdata/IC/subfile/MERRA2_ana_IC_????01150_interpolated.nc; do


#for ncdata in /n/regal/kuang_lab/nedkleiner/cesm_out1put/1rest??/rest/0001-01-16-00000/1rest??.cam.r.0001-01-16-00000.nc; do

 cd ~pchan/spcam2_0-cesm1_1_1/scripts/
 a="a"
 casenum=$((casenum+1))
 casename=$(printf '%02d' $casenum)
 OUTCASE="$i$a$CASEprefix$casename"
 CASE="$a$CASEprefix$casename"
 # illustration of directories: http://www.cesm.ucar.edu/events/tutorials/2016/practical1-bertini.pdf p.48
 CASEROOT="${HOME}/cesm_caseroot/ens/${CASE}"  # TODO edit if dislike
 OUTPUT="/n/kuanglfs/${USER}/cesm_output/${OUTCASE}" #TODO "/n/regal/`id -gn`/$USER/cesm_output/${CASE}"
 #OUTPUT="/n/regal/`id -gn`/$USER/cesm_output/${CASE}"
 RUNDIR="${OUTPUT}/run"
 EXEROOT="${OUTPUT}/bld"
 echo $CASEROOT

# ./create_newcase -case ${CASEROOT} -mach ${MACHINE} -res ${RES} -compset ${COMPSET} || exit -1
if [ $i == 1 ]; then
 ./create_clone -case ${CASEROOT} -clone ${CLONEROOT} || exit -1
 cp -a $0 $CASEROOT/
fi
# cp -a $SCRIPTDIR/Tools/mkbatch.${MACHINE} $CASEROOT/Tools/
#cloned cp -a $SCRIPTDIR/user_nl_* $CASEROOT/
 # Copy source modifications
#cloned cp -a $SCRIPTDIR/SourceMods/* $CASEROOT/SourceMods/

 cd $CASEROOT

if [ $i == 1 ]; then
 cat >> user_nl_cam <<EOF
ncdata = '$ncdata'
EOF
 sed -i -e 's/no-requeue/requeue/g' $CASE.run
fi
 # http://www.cesm.ucar.edu/models/cesm1.1/cesm/doc/modelnl/env_run.html

# sed -i "s/180/${NCORES}/g" env_mach_pes.xml
#TODO ./xmlchange NTASKS_ATM=${NCORES},NTASKS_LND=${NCORES},NTASKS_ICE=${NCORES},NTASKS_OCN=${NCORES},NTASKS_CPL=${NCORES},NTASKS_GLC=${NCORES},NTASKS_ROF=${NCORES}

 # Partition
# sed -i 's/\(#SBATCH -p \).*$/\1huce_amd/' Tools/mkbatch.${MACHINE}  #TODO
# ./xmlchange MAX_TASKS_PER_NODE=64  #TODO
 sed -i 's/\(#SBATCH -p \).*$/\1huce_intel/' Tools/mkbatch.${MACHINE}
 ./xmlchange MAX_TASKS_PER_NODE=28
# sed -i 's/\(#SBATCH --mem-per-cpu=\).*$/\11000/' Tools/mkbatch.${MACHINE}

 # Email when job ends
# sed -i 's/#\(#SBATCH --mail-type\)/\1/' Tools/mkbatch.${MACHINE}  #TODO turn on
# sed -i 's/^\(#SBATCH --mail-type\)/#\1/' Tools/mkbatch.${MACHINE}  #TODO turn off

# ./cesm_setup -clean
 # storage, work against hardcoded dirs
# ./xmlchange DIN_LOC_ROOT="/n/kuanglfs/pchan/CAM_input"  #don't change
 ./xmlchange -file env_build.xml -id EXEROOT -val $EXEROOT
 ./xmlchange -file env_run.xml -id RUNDIR -val $RUNDIR
 ./xmlchange -file env_run.xml -id DOUT_S -val TRUE  #TODO
 ./xmlchange -file env_run.xml -id DOUT_S_ROOT -val $OUTPUT

 grep ${CLONEROOT##*/} env_*.xml
 if [ $? -eq 0 ]; then
   echo not clean, exitting..
   exit -1
 fi

 # Set up for a 3-day run:
 ./xmlchange -file env_run.xml -id STOP_OPTION -val nsteps
 ./xmlchange -file env_run.xml -id STOP_N -val 6 #TODO
 ./xmlchange -file env_run.xml -id GET_REFCASE -val TRUE  #or it will check input data from svn...
# ./xmlchange -file env_run.xml -id RESUBMIT -val 2  #times of submit = RESUBMIT+1

# Get restart files:
 ./xmlchange -file env_run.xml -id REST_OPTION -val nhours
 ./xmlchange -file env_run.xml -id REST_N -val 24
 ./xmlchange -file env_run.xml -id DOUT_S_SAVE_ROOT -val $RUNDIR

 # continue run not a new one?
 ./xmlchange -file env_run.xml -id CONTINUE_RUN -val FALSE  # false: new run

 # restart files
 #./xmlchange -file env_run.xml -id REST_N -val 1
# ./xmlchange -file env_run.xml -id DOUT_S_SAVE_INT_REST_FILES -val TRUE  #TODO retain intermed restart
 #./xmlchange -file env_run.xml -id DOUT_S_SAVE_ALL_ON_DISK -val TRUE
 #./xmlchange -file env_run.xml -id DOUT_S_SAVE_ROOT -val $RUNDIR
   ##this is to save extra restart file

 # branch run (http://www.cesm.ucar.edu/models/cesm1.1/cesm/doc/usersguide/ug.pdf#use_case_branch)
 BRANCH=$i
 if [ $BRANCH == 1 ] ; then
	echo $BRANCH one
   caseminus=$(($casenum-1))
   branchname=$(printf '%02d' $caseminus)
   BRCPATH="/n/home08/nedkleiner/kuanglfs/MERRAfolder/hour0"
   BRCCASE="MERRAthreehr$branchname""_0"
   BRCDATE="0001-01-16"  #set before
   ./xmlchange -file env_run.xml -id RUN_TYPE -val "branch"
   ./xmlchange -file env_run.xml -id RUN_REFCASE -val $BRCCASE
   ./xmlchange -file env_run.xml -id RUN_REFDATE -val $BRCDATE
   ./xmlchange -file env_run.xml -id GET_REFCASE -val FALSE
	month=1 
fi

if [ $BRANCH -gt 1 ] ; then
	branchminus=$(($BRANCH-1))
	branchname2=$(printf '%02d' $casenum)
	BRCPATH="/n/kuanglfs/nedkleiner/cesm_output"
	BRCCASE="$branchminus""b$CASEprefix$branchname2"
	BRCCASE2="b$CASEprefix$branchname2"
	while [ ! -d $BRCPATH/$BRCCASE/rest ]; do
		sleep 1m
	done
	branchtime=$(($BRANCH*10800-10800))
	date=16
	month=1
	while [ $branchtime -gt 86399 ]
do
		branchtime=$(($branchtime-86400))
		date=$(($date+1))
	done
echo "got here"
trigger=0
while [ $(($date-$trigger)) -gt 28 ]; do
if [ $month = 4 ] || [ $month = 6 ] || [ $month = 9 ] || [ $month = 11 ] && [ $date -gt 30 ]; then
month=$(($month+1))
date=$(($date-30))
echo "thirty days..."
elif [ $month = 2 ]; then
month=$(($month+1))
date=$(($date-28))
echo "except february..."
elif [ $date -gt 31 ]; then
month=$(($month+1))
date=$(($date-31))
echo "all the rest..."
else
echo "triggered"
trigger=30
fi
done
datename=$(printf '%02d' $date)
monthname=$(printf '%02d' $month)
	BRCDATE="0001-$monthname-$datename"

   ./xmlchange -file env_run.xml -id RUN_REFCASE -val $BRCCASE2
   ./xmlchange -file env_run.xml -id RUN_REFDATE -val $BRCDATE
   ./xmlchange -file env_run.xml -id GET_REFCASE -val FALSE 
   ./xmlchange -file env_run.xml -id RUN_TYPE -val "branch"
fi 

if [ $i == 1 ]; then
 ./cesm_setup -clean
 ./cesm_setup
else

mkdir $OUTPUT
mkdir $EXEROOT
mkdir $RUNDIR

 #build
fi
 cp -a $clonebld/cesm.exe $EXEROOT/
 echo $EXEROOT "exeroot"

 ./xmlchange -file env_build.xml -id BUILD_COMPLETE -val TRUE  # fool *.run
 cp -a $ncdata ${RUNDIR}/
 #branch: copy restart file
 if [ $BRANCH == 1 ] ; then
   cp -a ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-00000/rpointer.* ${RUNDIR}/
   ln -sf ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-00000/${BRCCASE}.* ${RUNDIR}/
   echo branch from ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-00000/
 fi


 if [ $BRANCH  -gt 1 ] ; then
	branchsec=$(printf '%05d' $branchtime)
   cp -a ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-$branchsec/rpointer.* ${RUNDIR}/
   ln -sf ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-$branchsec/${BRCCASE2}.* ${RUNDIR}/
	./xmlchange -file env_run.xml -id RUN_REFTOD -val $branchsec
   echo branch from ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-$branchsec/
#   sec=$((i*3600+3600))
 fi

 # echo
 grep -Ev '^\s*$|^\s*!' user_nl_*
 \ls -l SourceMods/*/*

 # run file changes
# sed -i "s/P00000000/mjfu/g" ${CASE}.run
# sed -i "s/ptile=15/ptile=16/g" ${CASE}.run
# sed -i "s/8:00/12:00/g" ${CASE}.run
./${CASE}.submit
#JOB=`./${CASE}.submit|egrep -o -e "\b[0-9]+$"`
#JOBS="$JOBS:$JOB"
#sec=$((i*3600))
sec=$(($i*10800))
psec=$(printf '%05d' $sec)
date=16
month=1
while [ $sec -gt 86399 ]
do
	sec=$(($sec-86400))
	psec=$(printf '%05d' $sec)
	date=$(($date+1))
done



trigger=0
while [ $(($date-$trigger)) -gt 28 ]; do
if [ $month = 4 ] || [ $month = 6 ] || [ $month = 9 ] || [ $month = 11 ] && [ $date -gt 30 ]; then
month=$(($month+1))
date=$(($date-30))
elif [ $month = 2 ]; then
month=$(($month+1))
date=$(($date-28))
elif [ $date -gt 31 ]; then
month=$(($month+1))
date=$(($date-31))
else
trigger=30
fi
done
datename=$(printf '%02d' $date)
monthname=$(printf '%02d' $month)
trigger=0
NCES="$NCES ""$OUTPUT/atm/hist/$CASE.cam.h1.0001-$monthname-$datename-$psec.nc"
#NCES="$NCES ""$OUTPUT/atm/hist/$CASE.cam.h1.0001-01-16-10800.nc"
done

for j in $(seq 1 38); do
pj=$(printf '%02d' $j)
while [ ! -f "/n/kuanglfs/${USER}/cesm_output/$i$a$CASEprefix$pj/atm/hist/$a$CASEprefix$pj.cam.h1.0001-$monthname-$datename-$psec.nc" ];  do
echo waiting "/n/kuanglfs/${USER}/cesm_output/$i$a$CASEprefix$pj/atm/hist/$a$CASEprefix$pj.cam.h1.0001-$monthname-$datename-$psec.nc"
sleep 15s
done
done

cd ~

mv ~/currentavg.nc ~/avgfolder/currentavg$i
#rename currentavg $CASE ~/avgfolder/currentavg.nc

srun -p huce_intel -t 0:03:00 nces $NCES ~/currentavg.nc

while [ ! -f ~/currentavg.nc ]; do
	sleep 30s
done

rm ~/forcing_txt/wide/*_drft.txt

while [ -f ~/forcing_txt/wide/Z_drft.txt ]; do
	sleep 15s
done

sbatch ~/runtextwrite8.sh  # "--dependency=afterok$JOBS"

while [ ! -f ~/forcing_txt/wide/T_drft.txt ]; do
	sleep 1m
done

casenum=0
 cd $CLONEROOT2
 clonebld=`./xmlquery EXEROOT -s -valonly`

# fo/n/regal/kuang_lab/leiw/MERRA2_ANA/IC/MERRA2_ana_IC_????0115_interpolated.nc; do
for ncdata in ~nedkleiner/kuanglfs/MERRAdata/IC/subfile/MERRA2_ana_IC_????01150_interpolated.nc; do


#for ncdata in /n/regal/kuang_lab/nedkleiner/cesm_out1put/1rest??/rest/0001-01-16-00000/1rest??.cam.r.0001-01-16-00000.nc; do

 cd ~pchan/spcam2_0-cesm1_1_1/scripts/
 b="b"
 casenum=$((casenum+1))
 casename=$(printf '%02d' $casenum)
 OUTCASE="$i$b$CASEprefix$casename"
 CASE="$b$CASEprefix$casename"
 # illustration of directories: http://www.cesm.ucar.edu/events/tutorials/2016/practical1-bertini.pdf p.48
 CASEROOT="${HOME}/cesm_caseroot/ens/${CASE}"  # TODO edit if dislike
 OUTPUT="/n/kuanglfs/${USER}/cesm_output/${OUTCASE}" #TODO "/n/regal/`id -gn`/$USER/cesm_output/${CASE}"
 #OUTPUT="/n/regal/`id -gn`/$USER/cesm_output/${CASE}"
 RUNDIR="${OUTPUT}/run"
 EXEROOT="${OUTPUT}/bld"
 echo $CASEROOT

# ./create_newcase -case ${CASEROOT} -mach ${MACHINE} -res ${RES} -compset ${COMPSET} || exit -1
if [ $i == 1 ]; then
 ./create_clone -case ${CASEROOT} -clone ${CLONEROOT2} || exit -1
 cp -a $0 $CASEROOT/
fi

# cp -a $SCRIPTDIR/Tools/mkbatch.${MACHINE} $CASEROOT/Tools/
#cloned cp -a $SCRIPTDIR/user_nl_* $CASEROOT/
 # Copy source modifications
#cloned cp -a $SCRIPTDIR/SourceMods/* $CASEROOT/SourceMods/

 cd $CASEROOT
if [ $i == 1 ]; then  #TODO change back
 cat >> user_nl_cam <<EOF
ncdata = '$ncdata'
EOF
# sed -i -e 's/no-requeue/requeue/g' $CASE.run
fi

 # http://www.cesm.ucar.edu/models/cesm1.1/cesm/doc/modelnl/env_run.html

# sed -i "s/180/${NCORES}/g" env_mach_pes.xml
#TODO ./xmlchange NTASKS_ATM=${NCORES},NTASKS_LND=${NCORES},NTASKS_ICE=${NCORES},NTASKS_OCN=${NCORES},NTASKS_CPL=${NCORES},NTASKS_GLC=${NCORES},NTASKS_ROF=${NCORES}

 # Partition
# sed -i 's/\(#SBATCH -p \).*$/\1huce_amd/' Tools/mkbatch.${MACHINE}  #TODO
# ./xmlchange MAX_TASKS_PER_NODE=64  #TODO
 sed -i 's/\(#SBATCH -p \).*$/\1huce_intel/' Tools/mkbatch.${MACHINE}
 ./xmlchange MAX_TASKS_PER_NODE=28
# sed -i 's/\(#SBATCH --mem-per-cpu=\).*$/\11000/' Tools/mkbatch.${MACHINE}

 # Email when job ends
# sed -i 's/#\(#SBATCH --mail-type\)/\1/' Tools/mkbatch.${MACHINE}  #TODO turn on
# sed -i 's/^\(#SBATCH --mail-type\)/#\1/' Tools/mkbatch.${MACHINE}  #TODO turn off

# ./cesm_setup -clean
 # storage, work against hardcoded dirs
# ./xmlchange DIN_LOC_ROOT="/n/kuanglfs/pchan/CAM_input"  #don't change
 ./xmlchange -file env_build.xml -id EXEROOT -val $EXEROOT
 ./xmlchange -file env_run.xml -id RUNDIR -val $RUNDIR
 ./xmlchange -file env_run.xml -id DOUT_S -val TRUE  #TODO
 ./xmlchange -file env_run.xml -id DOUT_S_ROOT -val $OUTPUT

 grep ${CLONEROOT2##*/} env_*.xml
 if [ $? -eq 0 ]; then
   echo not clean, exitting..
   exit -1
 fi

 # Set up for a 3-day run:
 ./xmlchange -file env_run.xml -id STOP_OPTION -val nsteps
 ./xmlchange -file env_run.xml -id STOP_N -val 6 #TODO
 ./xmlchange -file env_run.xml -id GET_REFCASE -val TRUE  #or it will check input data from svn...
# ./xmlchange -file env_run.xml -id RESUBMIT -val 2  #times of submit = RESUBMIT+1

# Get restart files:
 ./xmlchange -file env_run.xml -id REST_OPTION -val nsteps
 ./xmlchange -file env_run.xml -id REST_N -val 6
 ./xmlchange -file env_run.xml -id DOUT_S_SAVE_ROOT -val $RUNDIR

 # continue run not a new one?
 ./xmlchange -file env_run.xml -id CONTINUE_RUN -val FALSE  # false: new run

 # restart files
 #./xmlchange -file env_run.xml -id REST_N -val 1
# ./xmlchange -file env_run.xml -id DOUT_S_SAVE_INT_REST_FILES -val TRUE  #TODO retain intermed restart
 #./xmlchange -file env_run.xml -id DOUT_S_SAVE_ALL_ON_DISK -val TRUE
 #./xmlchange -file env_run.xml -id DOUT_S_SAVE_ROOT -val $RUNDIR
   ##this is to save extra restart file

 # branch run (http://www.cesm.ucar.edu/models/cesm1.1/cesm/doc/usersguide/ug.pdf#use_case_branch)
 BRANCH=$i
if [ $BRANCH == 1 ] ; then
	echo $BRANCH one
   caseminus=$(($casenum-1))
   branchname=$(printf '%02d' $caseminus)
   BRCPATH="/n/home08/nedkleiner/kuanglfs/MERRAfolder/hour0"
   BRCCASE="MERRAthreehr$branchname""_0"
   BRCDATE="0001-01-16"  #set before
   ./xmlchange -file env_run.xml -id RUN_TYPE -val "branch"
   ./xmlchange -file env_run.xml -id RUN_REFCASE -val $BRCCASE
   ./xmlchange -file env_run.xml -id RUN_REFDATE -val $BRCDATE
   ./xmlchange -file env_run.xml -id GET_REFCASE -val FALSE  
fi

if [ $BRANCH -gt 1 ] ; then
	branchminus=$(($BRANCH-1))
	branchname2=$(printf '%02d' $casenum)
	BRCPATH="/n/kuanglfs/nedkleiner/cesm_output"
	BRCCASE="$branchminus""b$CASEprefix$branchname2"
	BRCCASE2="b$CASEprefix$branchname2"
	branchtime=$(($BRANCH*10800-10800))
	date=16
	month=1
	while [ $branchtime -gt 86399 ]
	do
		branchtime=$(($branchtime-86400))
		date=$(($date+1))
done
echo $date

trigger=0
while [ $(($date-$trigger)) -gt 28 ]; do
if [ $month = 4 ] || [ $month = 6 ] || [ $month = 9 ] || [ $month = 11 ] && [ $date -gt 30 ]; then
month=$(($month+1))
date=$(($date-30))
echo "thirty days..."
elif [ $month = 2 ]; then
month=$(($month+1))
date=$(($date-28))
echo "except february..."
elif [ $date -gt 31 ]; then
month=$(($month+1))
date=$(($date-31))
echo "all the rest..."
else
echo "triggered"
trigger=30
fi
done
datename=$(printf '%02d' $date)
monthname=$(printf '%02d' $month)
	BRCDATE="0001-$monthname-$datename"
	./xmlchange -file env_run.xml -id RUN_REFCASE -val $BRCCASE2
	./xmlchange -file env_run.xml -id RUN_REFDATE -val $BRCDATE
   ./xmlchange -file env_run.xml -id GET_REFCASE -val FALSE 
   ./xmlchange -file env_run.xml -id RUN_TYPE -val "branch"
   ./xmlchange -file env_run.xml -id BRNCH_RETAIN_CASENAME -val TRUE
#Set BRNCH_RETAIN_CASENAME to TRUE in env_run.xml
fi 
# DEBUG
 #./xmlchange -file env_build.xml -id DEBUG -val TRUE
 #./xmlchange -file env_run.xml -id INFO_DBUG -val 3

 #chemistry
 #./xmlchange -file env_build.xml -id CAM_CONFIG_OPTS -val -chem trop_mam3
 #namelist variable
 # Now namelist variables can only be changed in usr_nl_cam etc.

 # If using a non-standard SST forcing, uncomment this and fill in the path:
 #./xmlchange -file env_run.xml -id SSTICE_DATA_FILENAME -val /glade/scratch/mjfu/SST_4xCO2/finalBC_CESM/sstice_clim.nc
 ######./xmlchange -file env_run.xml -id DOCN_SSTDATA_FILENAME -val /glade/scratch/mjfu/SST_4xCO2/finalBC_CESM/sstice_clim.nc

 # If wanting to change CO2 concentration
 #./xmlchange -file env_run.xml -id CCSM_CO2_PPMV -val 1148
# ./xmlchange -file env_run.xml -id CAM_NAMELIST_OPTS -val solar_const=700.  #TODO pchan only

 # change timestep set atm_cpl_dt...(DRV namelist  seq_timemgr_inparm)to change dtime, and atm_cpl_dt is changed by modifying ATM_NCPL(default 48) in env_run.xml
 #./xmlchange -file env_run.xml -id ATM_NCPL -val 8640
 #./xmlchange -file env_run.xml -id GLC_NCPL -val 8640

 # If not using the default deep convection parameterization zmconv_...
 # Then change it from the Buildconf/cam.buildnml.csh
 # Not suggest to change  CAM_CONFIG_OPTS any more!!! cannot change nml there!
 #./xmlchange -file env_build.xml -id CAM_CONFIG_OPTS -val "-nlev 56"


 #configure utilities of CAM (work for CAM only) Not Suggested! will change original set
 #/n/home05/mjfu/cesm1_2_0/models/atm/cam/bld/configure -chem waccm_ghg -nlev 66 -waccm_phys -test

# ./xmlchange -file env_build.xml -id GMAKE_J -val 4
 #configure
if [ $i == 1 ]; then
 ./cesm_setup -clean
 ./cesm_setup

else

mkdir $OUTPUT
mkdir $EXEROOT
mkdir $RUNDIR
fi

 #build
 cp -a $clonebld/cesm.exe $EXEROOT/
# cp -a $clonebld/* $EXEROOT/
 # sed required if re-build needed
 # Deppath Filepath Makefile.conf* config.* *.bldlog.*
# ./${CASE}.build
 ./xmlchange -file env_build.xml -id BUILD_COMPLETE -val TRUE  # fool *.run

 cp -a $ncdata ${RUNDIR}/
 #branch: copy restart file
 if [ $BRANCH == 1 ] ; then
   cp -a ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-00000/rpointer.* ${RUNDIR}/
   ln -sf ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-00000/${BRCCASE}.* ${RUNDIR}/
   echo branch from ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-00000/
 fi


 if [ $BRANCH -gt 1 ] ; then
	branchsec=$(printf '%05d' $branchtime)
   cp -a ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-$branchsec/rpointer.* ${RUNDIR}/
   ln -sf ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-$branchsec/${BRCCASE2}.* ${RUNDIR}/
	./xmlchange -file env_run.xml -id RUN_REFTOD -val $branchsec
   echo branch from ${BRCPATH}/${BRCCASE}/rest/${BRCDATE}-$branchsec/
 fi

 # echo
 grep -Ev '^\s*$|^\s*!' user_nl_*
 \ls -l SourceMods/*/*

 # run file changes
# sed -i "s/P00000000/mjfu/g" ${CASE}.run
# sed -i "s/ptile=15/ptile=16/g" ${CASE}.run
# sed -i "s/8:00/12:00/g" ${CASE}.run

 ./${CASE}.submit
done
sleep 1m

while [ ! -d $OUTPUT/atm ]; do
sleep 1m
done

cp ~/forcing_txt/wide/T_drft.txt ~/driftfolder/T_drft$i
cp ~/forcing_txt/wide/U_drft.txt ~/driftfolder/U_drft$i
cp ~/forcing_txt/wide/V_drft.txt ~/driftfolder/V_drft$i
cp ~/forcing_txt/wide/Q_drft.txt ~/driftfolder/Q_drft$i
cp ~/forcing_txt/wide/Z_drft.txt ~/driftfolder/Z_drft$i

#srun rm ~/cesm_caseroot/ens/*$CASEprefix* -r -f !($CASE)

done
# printf "To submit job, issue:\n"
# printf "cd ${CASEROOT}\n"
# printf "./${CASE}.submit\n"

# printf "To submit job, issue:\n"
# printf "cd ${CASEROOT}\n"
# printf "./${CASE}.submit\n"
